package com.fg.fgmall.service.impl;

import com.fg.fgmall.entity.Category;
import com.fg.fgmall.mapper.CagetoryMapper;
import com.fg.fgmall.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CagetoryMapper cagetoryMapper;

    @Override
    public List<Map<String, Object>> selectAllCategory() throws Exception {

        List<Map<String, Object>> categories = new ArrayList<>();
        List<Category> superCategories = cagetoryMapper.selectSupperCategory();
        for (Category category : superCategories) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", category.getId());
            map.put("name", category.getName());
            List<Category> children = cagetoryMapper.selectByParentId(category.getId());
            map.put("children", children);
            categories.add(map);
        }

        return categories;
    }
}
